<?php

	class Profile{
		private $users;
		
		// If the user was not logged in, redirect him to the homepage.
		public function load(){
			if(data('fb')->user === 0){
				header('Location: ' . URL);
			}
			
			// Set header data.
			$this->users = new Users();
			data('header', $this->users->read(data('fb')->user, array('id', 'firstName')));
		}
		
		public function __construct(){
			$this->users = new Users();
		}
		
		public function user($userId){
			if($this->users->exists($userId)){
				$userData = $this->users->read($userId, array('id', 'name', 'location', 'birthday', 'relationshipStatus', 'sigOtherId', 'isPublic'));
				
				// Make the private profiles actually private.
				if($userData['isPublic'] === '0'){
					if(!$this->users->isFriendWith(data('fb')->user, $userData['id'])){
						$this->app->throwError('profile_no_access');
					}
				}
				
				if($userData['sigOtherId'] !== null){
					if($this->users->exists($userData['sigOtherId'])){
						$userData['sigOther'] = $this->users->read($userData['sigOtherId'], array('id', 'name'));
					}
				}
				
				data('userData', $userData);
				displayView('user_profile');
			} else {
				$this->app->throwError('profile_no_hikle');
			}
		}
		
		public function company($id){
			data('profileType', 1);
			
			// Add the company to the cache of the user.
			$this->users->createSearchCacheObject(data('fb')->user, $id, 1);
			
			$locations = new Locations();
			data('locationData', $locations->readCompany($id));
			displayView('company_profile');
		}
		
		public function city($id){
			data('profileType', 3);
			
			// Add the city to the cache of the user.
			$this->users->createSearchCacheObject(data('fb')->user, $id, 3);
			
			$locations = new Locations();
			data('locationData', $locations->readCity($id));
			displayView('city_profile');
		}
		
//		public function location($compId){
//			if(strlen($compId) > 0 && $this->app->model('locations')->companyExists($compId)){
//				$locations = $this->app->model('locations');
//				$users = $this->app->model('users');
//				
//				// If the users looks at the profile page, we have to add the company to the user cache.
//				$users->addToSearchCache($this->fb->user, $compId, 1);
//				// $this->auth();
//				data('companyData', $locations->getCompany($compId));
//				// data('companyStats', $locations->getCompanyStats($compId));
//				data('companyId', $compId);
//			
//				// Header data.
//				data('firstFriend', $users->getFirstFriend($this->fb->user));
//				data('profileIcon', $this->fb->getProfilePictureLink($this->fb->user));
//				data('profileName', $users->getName(utf8_encode($this->fb->user)));
//				data('logoutUrl', $this->fb->authUrl);
//				
//				// The view.
//				displayView('company_profile');
//			}
//		}
	}

// File end.